/*
 *  Copyright (C) 2010 Matthias Buch-Kromann <mbk.isv@cbs.dk>
 * 
 *  This file is part of the IncrementalParser package.
 *  
 *  The IncrementalParser program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.osdtsystem.incparser.features;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Matthias Buch-Kromann <mbk.isv@cbs.dk>
 */
public class FeatureAggregatorFeatureVector extends AbstractFeatureAggregator {
    FeatureVector featureVector = null;
    FeatureVector nextOptimalFeatureVector, nextAlternativeFeatureVector;
    FeatureAggregatorScore scoreAggregator;
    List<FeatureVector> featureVectors = new ArrayList<FeatureVector>();
    
    public FeatureAggregatorFeatureVector(WeightVector weights,
            FeatureHandler featureHandler, int scorer) {
        super(featureHandler, scorer);
        scoreAggregator = new FeatureAggregatorScore(weights, featureHandler, scorer);
        featureVector = null;
        nextOptimalFeatureVector = new FeatureVectorSparse();
        nextAlternativeFeatureVector = new FeatureVectorSparse();
    }

    public void pushFeatureVector(FeatureVector fv) {
        this.featureVectors.add(featureVector);
        this.featureVector = fv;
    }

    public FeatureVector popFeatureVector() {
        FeatureVector fv = featureVector;
        this.featureVector = featureVectors.remove(featureVectors.size() - 1);
        return fv;
    }
    
    @Override
    public void clear() {
        scoreAggregator.clear();
        if (featureVector != null)
            featureVector.clear();
        nextOptimalFeatureVector.clear();
        nextAlternativeFeatureVector.clear();
    }

    // Features
    @Override
    public void addFeature(int feature) {
        addFeature(feature, 1);
    }

    @Override
    public final void addFeature(int feature, float weight) {
        scoreAggregator.addFeature(feature, weight);
        if (scoreAggregator.returnOptimum == FeatureAggregatorScore.NONE)
            featureVector.addWeight(feature, weight);
        else {
            nextAlternativeFeatureVector.addWeight(feature, weight);
            scoreAggregator.addFeature(feature, weight);
        }
    }

    // Optimal features
    @Override
    public void openOptimalFeatureSequenceSpecification(boolean maximum) {
        scoreAggregator.openOptimalFeatureSequenceSpecification(maximum);
        nextOptimalFeatureVector.clear();
        nextAlternativeFeatureVector.clear();
    }

    @Override
    public boolean submitAlternativeFeatureSequence() {
        if (scoreAggregator.submitAlternativeFeatureSequence()) {
            FeatureVector swap = nextOptimalFeatureVector;
            nextOptimalFeatureVector = nextAlternativeFeatureVector;
            nextAlternativeFeatureVector = swap;
            swap.clear();
            return true;
        }
        return false;
    }

    @Override
    public void closeOptimalFeatureSequenceSpecification() {
        scoreAggregator.closeOptimalFeatureSequenceSpecification();
        nextOptimalFeatureVector.addTo(1, featureVector);
    }


    // Other
    @Override
    public void trimToSize() {
        scoreAggregator.trimToSize();
        featureVector.trimToSize();
        nextOptimalFeatureVector.trimToSize();
        nextAlternativeFeatureVector.trimToSize();
    }
}
